package com.wangzhou.back.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wangzhou.common.util.DateUtil;
import com.wangzhou.common.util.EncryUtil;
import com.wangzhou.common.util.PageInfo;
import com.wangzhou.users.dao.IAAdminsDAO;
import com.wangzhou.users.dao.implay.AAdminsDAOImplay;
import com.wangzhou.users.pojo.AAdmins;

/**
 * 登陆后操作:
 * 用户模块
 * @author zjx
 *
 */
public class UsersBackServlet extends HttpServlet
{
	private IAAdminsDAO adminsDAO = new AAdminsDAOImplay() ; 
	private DateUtil dateUtil = new DateUtil();
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		//让一个servlet有多个功能请求
		String method = request.getParameter("method");
		if("main".equalsIgnoreCase(method))
		{
			this.main(request,response);
		}else if("leftMenu".equalsIgnoreCase(method))
		{
			this.leftMenu(request,response);
		}else if("logout".equalsIgnoreCase(method))
		{
			this.logout(request,response);
		}else if("adminsList".equalsIgnoreCase(method))
		{
			this.adminsList(request,response);
		}else if("adminsInsert".equalsIgnoreCase(method))
		{
			this.adminsInsert(request,response);
		}else if("adminsInsertSubmit".equalsIgnoreCase(method))
		{
			this.adminsInsertSubmit(request,response);
		}else if("adminsUpdate".equalsIgnoreCase(method))
		{
			this.adminsUpdate(request,response);
		}else if("adminsUpdateSubmit".equalsIgnoreCase(method))
		{
			this.adminsUpdateSubmit(request,response);
		}
	}
	
	/**
	 * 左侧导航
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void leftMenu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String url = "/WEB-INF/back/leftMenu.jsp"; 
		request.getRequestDispatcher(url).forward(request, response);
	}

	/**
	 * 登陆后的首页
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void main(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String url = "/WEB-INF/back/users/main.jsp" ;
		String info = "" ; 
		request.getRequestDispatcher(url).forward(request, response);
	}
	
	/**
	 * 管理员列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void adminsList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String url = "/WEB-INF/back/users/adminsList.jsp" ;
		String info = "" ; 
		//接收参数
		String currentPage = request.getParameter("currentPage");
		String pageSize = request.getParameter("pageSize");
		
		String keyword = request.getParameter("keyword");
		String st = request.getParameter("st");
		String ed = request.getParameter("ed");
		String status = request.getParameter("status");
		
		//处理参数
		if(keyword == null)
		{
			keyword = "" ; 
		}
		keyword = keyword.trim();
		if(st == null)
		{
			st = "" ; 
		}
		if(ed == null)
		{
			ed = "" ; 
		}
		if(status == null)
		{
			status = "" ; 
		}
		
		
		Date stDate = null ; 
		Date edDate = null ; 
		if(!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			//将字符串的时间转化为日期对象的时间
			stDate = this.dateUtil.parseDateTime(st);
			edDate = this.dateUtil.parseDateTime(ed);
		}
		
		//分页对象
		PageInfo pageInfo = new PageInfo();
		try
		{
			pageInfo.setCurrentPage(Integer.valueOf(currentPage));
			pageInfo.setPageSize(Integer.valueOf(pageSize));
		} catch (NumberFormatException e)
		{
			
		}
		
		//查询数据库
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("keyword", keyword);
		paramsMap.put("stDate", stDate);
		paramsMap.put("edDate", edDate);
		paramsMap.put("status", status);
		List<AAdmins> adminsList = this.adminsDAO.findCondList(pageInfo, "1", paramsMap);
		//存储对象,为了jsp页面获取
		request.setAttribute("pageInfo", pageInfo);
		request.setAttribute("adminsList", adminsList);
		request.getRequestDispatcher(url).forward(request, response);
	}
	
	/**
	 * 打开管理员添加页面
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void adminsInsert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String url = "/WEB-INF/back/users/adminsInsert.jsp" ;
		String info = "" ; 
		request.getRequestDispatcher(url).forward(request, response);
	}
	
	/**
	 * 管理员添加提交页面
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void adminsInsertSubmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String url = "/WEB-INF/back/users/main.jsp" ;
		String info = "" ; 
		//接收参数
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		String trueName = request.getParameter("trueName");
		String phone = request.getParameter("phone");
		String qq = request.getParameter("qq");
		//处理参数
		if(email == null)
		{
			email = "" ; 
		}
		if(password == null)
		{
			password = "" ; 
		}
		if(trueName == null)
		{
			trueName = "" ; 
		}
		if(phone == null)
		{
			phone = "" ; 
		}
		if(qq == null)
		{
			qq = "" ; 
		}
		//new对象
		AAdmins admins = new AAdmins();
		//赋值
		admins.setEmail(email);
		admins.setPassword(EncryUtil.getMD5(password));
		admins.setTrueName(trueName);
		admins.setPhone(phone);
		admins.setQq(qq);
		admins.setStatus(Byte.valueOf("1"));
		admins.setLoginCount(0);
		admins.setCreatetime(new Date());
		admins.setUpdatetime(new Date());
		admins.setLasttime(new Date());
		//操作数据库
		int res = this.adminsDAO.insert(admins);
		if(res > 0 )
		{
			//插入成功
			System.out.println("--插入成功--" + admins.getId());
		}else
		{
			//插件失败
			System.out.println("--插入失败--" + admins.getId());
		}
		//request.getRequestDispatcher(url).forward(request, response);
	}
	
	/**
	 * 打开管理员更新页面
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void adminsUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String url = "/WEB-INF/back/users/adminsUpdate.jsp" ;
		String info = "" ; 
		String aid = request.getParameter("aid");
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("id", aid);
		AAdmins admins = this.adminsDAO.findOne("1", paramsMap);
		if(admins != null)
		{
			request.setAttribute("admins", admins);
		}
		request.getRequestDispatcher(url).forward(request, response);
	}
	
	/**
	 * 管理员更新提交页面
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void adminsUpdateSubmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String url = "/WEB-INF/back/users/main.jsp" ;
		String info = "" ; 
		//接收参数
		String aid = request.getParameter("aid");
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("id", aid);
		AAdmins admins = this.adminsDAO.findOne("1", paramsMap);
		if(admins != null)
		{
			boolean flag = false ; 
			String operFlag = request.getParameter("operFlag");
			if("update".equalsIgnoreCase(operFlag))
			{
				String email = request.getParameter("email");
				String trueName = request.getParameter("trueName");
				String phone = request.getParameter("phone");
				String qq = request.getParameter("qq");
				//处理参数
				if(email == null)
				{
					email = "" ; 
				}
				if(trueName == null)
				{
					trueName = "" ; 
				}
				if(phone == null)
				{
					phone = "" ; 
				}
				if(qq == null)
				{
					qq = "" ; 
				}
				//赋值
				admins.setEmail(email);
				admins.setTrueName(trueName);
				admins.setPhone(phone);
				admins.setQq(qq);
				admins.setStatus(Byte.valueOf("1"));
				
				flag = true ; 
			}else if("updatePass".equalsIgnoreCase(operFlag))
			{
				//修改密码
			}else if("resetPass".equalsIgnoreCase(operFlag))
			{
				//重置密码
			}
			
			if(flag)
			{
				admins.setUpdatetime(new Date());
				admins.setLasttime(new Date());
				//操作数据库
				int res = this.adminsDAO.update("1",admins);
				if(res > 0 )
				{
					//插入成功
					System.out.println("--更新成功--" + admins.getId());
				}else
				{
					//插件失败
					System.out.println("--更新失败--" + admins.getId());
				}
				request.setAttribute("admins", admins);
			}else
			{
				//把错误信息传给前台
			}
		}
	}
	
	/**
	 * 退出
	 * @param request
	 * @param response
	 */
	private void logout(HttpServletRequest request, HttpServletResponse response)
	{
		
	}
}
